var oldMenuIndex = '001';

document.ready(function() {
    getItem('webIframe').height = document.documentElement.clientHeight - 88;

    var token = getStorage('user_token');
    if (token == null || token == '') {
        toUrl('console.php?m=account&c=login');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'getOwnerInfo';

    sendAjax(param, function(result) {
        if (result.res == 1) {
            //加载顶部菜单导航与左侧菜单
            setValue('userid', result.userid);
            setValue('firstMenuId', result.menu[0].id);

            loadTopMenu(result.menu, result.name);
            loadLeftMenu(result.menu);
            loadOwnerMenu(result.owner);

            oldMenuIndex = getValue('firstMenuId');

            getWaitWorkCount();
        }
        else {
            showTip(result.msg);
            setTimeout(function() {
                setStorage('user_token', '');
                delStorage('duty_token');
                toUrl('console.php?m=account&c=login');
            }, 2000);
        }
    }, true);
});

function getWaitWorkCount() {
    var param = {};
    param.ctrl = 'account';
    param.func = 'getWaitWorkCount';

    sendAjax(param, function(result) {
        setWaitWorkCount('subMenuNum_record_report', result.waitReportCount);
        setMainMenuDot();
    });
}

function setWaitWorkCount(subMenuNumId, count) {
    if (getItem(subMenuNumId) == null) {
        return;
    }

    setHtml(subMenuNumId, count);
    if (getHtml(subMenuNumId) != '0') {
        setDisplay(subMenuNumId, '');
    }
    else {
        setDisplay(subMenuNumId, 'none');
    }
}

function setMainMenuDot() {
    var barList = getItem('leftMenu').children;
    for (var i = 0; i < barList.length; i++) {
        var menuList = barList[i].children;
        for (var m = 0; m < menuList.length; m++) {
            if (menuList[m].className != 'sub-menu-bar' && menuList[m].id != 'mainMenu0') {
                var flag = 0;
                var index = menuList[m].id.replace('mainMenu', '');
                var subMenuList = getItem('subMenuBar' + index).children;
                for (var n = 0; n < subMenuList.length; n++) {
                    var obj = subMenuList[n].children[2];
                    if (obj.style.display == '') {
                        flag = 1;
                        break;
                    }
                }

                menuList[m].children[2].style.display = flag == 1 ? '' : 'none';
            }
        }
    }
}

function openMenu() {
    setDisplay('maskFloatDiv', '');
    document.body.style.overflow = 'hidden';
    setClass('menuBar', 'menu-bar-out');
    setValue('flag', 'menu');
}

function closeMenu() {
    setDisplay('maskFloatDiv', 'none');
    document.body.style.overflow = 'visible';
    setClass('menuBar', 'menu-bar-in');
    setValue('flag', '');
}

function openSetup() {
    setDisplay('maskFloatDiv', '');
    document.body.style.overflow = 'hidden';
    setClass('setupBar', 'setup-bar-out');
    setValue('flag', 'setup');
}

function closeSetup() {
    setDisplay('maskFloatDiv', 'none');
    document.body.style.overflow = 'visible';
    setClass('setupBar', 'setup-bar-in');
    setValue('flag', '');
}

function closeMask() {
    if (getValue('flag') == 'menu') {
        closeMenu();
    }
    else {
        closeSetup();
    }
}

function loadTopMenu(menu, name) {
    var topMenu = '';
    for (var i = 0; i < menu.length; i++) {
        var topMenuClass = (i == 0 ? 'tab-menu-click' : 'tab-menu');

        if (menu[i].useto == 0) {
            if (menu[i].grade == 0) {
                topMenu += '<div id="mainMenuNav' + menu[i].id + '" class="' + topMenuClass + '" onclick="showMainMenuBar(' + menu[i].id + ')">' + menu[i].name + '</div>';
            }
            else {
                topMenu += '<div id="mainMenuNav' + menu[i].id + '" class="' + topMenuClass + '" onclick="openHome(\'' + menu[i].url + '\', \'' + menu[i].mode + '\')">' + menu[i].name + '</div>';
            }
        }
    }

    setHtml('optionTab', topMenu);
}

function loadLeftMenu(menu) {
    var leftMenu = '';
    var count = 0;

    for (var i = 0; i < menu.length; i++) {
        if (menu[i].grade == 0 && menu[i].useto == 0) {
            leftMenu += '<div id="mainMenuBar' + menu[i].id + '" ' + (i != 0 ? 'style="display: none;"' : '') + '>';

            if (i == 0) {
                leftMenu += '<div id="mainMenu0" class="left-menu-click" onclick="showSubMenu(\'0\', \'console.php?m=account&c=home\')">';
                leftMenu += '<img class="menu-icon l1" src="static/pic/pc/home.png" />';
                leftMenu += '<div class="menu-text l2">首页</div>';
                leftMenu += '</div>';
            }

            var parentMenu = menu[i].parent;
            if (parentMenu != null && parentMenu != '') {
                for (var m = 0; m < parentMenu.length; m++) {
                    if (parentMenu[m].useto == 0) {
                        count++;
                        leftMenu += '<div id="mainMenu' + count + '" class="left-menu" onclick="showSubMenu(\'' + count + '\')">';
                        leftMenu += '<img class="menu-icon l1" src="' + parentMenu[m].icon + '" />';
                        leftMenu += '<div class="menu-text l2">' + parentMenu[m].name + '</div>';
                        leftMenu += '<div class="menu-dot" style="display: none;"></div>';
                        leftMenu += '<img id="imgClick' + count + '" class="menu-img r1" src="static/pic/pc/left.png" />';
                        leftMenu += '</div>';
                        leftMenu += '<div id="subMenuBar' + count + '" class="sub-menu-bar" style="display: none;">';

                        var childMenu = parentMenu[m].child;
                        if (childMenu != null && childMenu != '') {
                            for (var n = 0; n < childMenu.length; n++) {
                                if (childMenu[n].useto == 0) {
                                    leftMenu += '<div class="sub-menu" onclick="selectMenu(this, \'' + childMenu[n].url.replaceAll('admin.php', 'console.php') + '\')">';
                                    leftMenu += '<img class="sub-menu-icon l3" src="' + childMenu[n].icon + '" />';
                                    leftMenu += '<div class="sub-menu-text l4">' + childMenu[n].name + '</div>';
                                    leftMenu += '<div class="sub-menu-num" style="display: none;" id="' + getSubMenuNumId(childMenu[n].url) + '"></div>';
                                    leftMenu += '</div>';
                                }
                            }
                        }

                        leftMenu += '</div>';
                    }
                }
            }

            leftMenu += '</div>';
        }
    }

    setHtml('leftMenu', leftMenu);
    setValue('parentMenuCount', count);
}

function loadOwnerMenu(owner) {
    if (owner.duty1689838063 != 'yes') {
        setDisplay('duty', 'none');
    }
    if (owner.wechat1689838063 != 'yes') {
        setDisplay('wechat', 'none');
    }
}

function getSubMenuNumId(url) {
    var param = url.split('?')[1];
    var list = param.split('&');
    return 'subMenuNum_' + list[0].replaceAll('m=', '') + '_' + list[1].replaceAll('c=', '');
}

function hideSubMenu() {
    var count = parseInt(getValue('parentMenuCount'));
    for (i = 1; i < count; i++) {
        setDisplay('subMenuBar' + i, 'none');
        getItem('imgClick' + i).src = 'static/pic/pc/left.png';
    }
}

var oldIndex = 0;
var oldObj = null;
function showSubMenu(newIndex) {
    if (oldObj != null) {
        oldObj.className = 'sub-menu';
    }
    
    if (newIndex == 0) {
        setClass('mainMenu' + oldIndex, 'left-menu');
        setClass('mainMenu' + newIndex, 'left-menu-click');
        hideSubMenu();
        getItem('webIframe').src = arguments[1];
        setHtml('title', '运营管理');
        closeMenu();
        oldIndex = 0;
        return;
    }

    if (oldIndex == newIndex) {
        setClass('mainMenu' + oldIndex, 'left-menu');
        setDisplay('subMenuBar' + oldIndex, 'none');
        getItem('imgClick' + oldIndex).src = 'static/pic/pc/left.png';
        oldIndex = 0;
        return;
    }

    hideSubMenu();

    if (newIndex != 0) {
        setDisplay('subMenuBar' + newIndex, '');
        getItem('imgClick' + newIndex).src = 'static/pic/pc/down.png';
    }

    setClass('mainMenu' + oldIndex, 'left-menu');
    setClass('mainMenu' + newIndex, 'left-menu-click');

    oldIndex = newIndex;

    if (arguments[1]) {
        getItem('webIframe').src = arguments[1];
        setHtml('title', '运营管理');
        closeMenu();
    }
}

function selectMenu(newObj, url) {
    if (oldObj != null) {
        oldObj.className = 'sub-menu';
    }
    newObj.className = 'sub-menu-click';
    oldObj = newObj;

    getItem('webIframe').src = url;

    setHtml('title', newObj.children[1].innerHTML);
    closeMenu();
}

function showMainMenuBar(newMenuIndex) {
    if (oldMenuIndex == newMenuIndex) {
        return;
    }

    setDisplay('mainMenuBar' + newMenuIndex, '');
    setDisplay('mainMenuBar' + oldMenuIndex, 'none');
    setClass('mainMenuNav' + newMenuIndex, 'tab-menu-click');
    setClass('mainMenuNav' + oldMenuIndex, 'tab-menu');

    oldMenuIndex = newMenuIndex;
}

function openHome(url, type) {
    toUrl(url);
}

function intoMenu(obj, url) {
    if (isEmpty('userid')) {
        showTip('滞留时间过长，请重新登录');
        setTimeout(function() {
            setStorage('user_token', '');
            delStorage('duty_token');
            toUrl('console.php?m=account&c=login');
        }, 2000);
        return;
    }

    var param = '&userid=' + getValue('userid');
    getItem('webIframe').src = url + param;

    setHtml('title', obj.innerHTML);
    closeSetup();
}

function exit() {
    closeSetup();

    openConfirm('确定退出系统吗？', function() {
        setStorage('user_token', '');
        delStorage('duty_token');
        delStorage('helper_autologin');
        toUrl('console.php?m=account&c=login');
    });
}